<?php
/**
* @package   auto_occasion
* @subpackage actu
* @author    your name
* @copyright 2011 your name
* @link      http://www.yourwebsite.undefined
* @license    All rights reserved
*/

class BoActuCtrl extends jController {

    public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
                            );

    /**
    * Liste des categories des actus
    */
    function index() {
        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_ACTU ;
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validation.js');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.ui.core.js');

        jClasses::inc('actu~actuSrv');
        jClasses::inc('actu~actuCategorieSrv');
        jClasses::inc('actu~motcleSrv');
        
        $id    = $this->intParam('id', '');
        $idCat = $this->intParam('idCat', '');

        $actuByCategorie = ($idCat)? actuSrv::listeByCategorie($idCat) : array();
        $cat             = actuCategorieSrv::liste();
        $motcle          = motcleSrv::liste();
        //print_r($actuByCategorie);die();

        $actu = ($id)? actuSrv::chargeParId($id) : actuSrv::init();

        if ($actu->ao_actu_content) {
            $content = $actu->ao_actu_content;
            $filename = JELIX_APP_WWW_PATH . PATH_ACTU_TXT . $content;
            if (strpos($content, ".txt") !== false) {
                //récupération du contenu du fichier et le mettre dans la variable ess_zChapeau x :gamme-porsche-1019663438.txt
                jClasses::inc('commun~toolsFile');
                $actu->ao_actu_content = toolsFile::get($filename);
            }
        }

        
        $rep->bodyTpl = 'actu~BoActuList';
        
        $rep->body->assign('idCat', $idCat);
        $rep->body->assign('actu', $actu);
        $rep->body->assign('motcle', $motcle);
        $rep->body->assign('actuByCategorie', $actuByCategorie);
        $rep->body->assign('cat', $cat);

        return $rep;
    }

    

    /**
     * Enregistrer une actualité
     *
     *
     */
     function sauvegarder() {

        jClasses::inc("actu~actuSrv");
        jClasses::inc("commun~communSrv");

        $id                = $this->intParam ('id', 0);

        $params = new stdClass();
        $params->ao_actu_name           = $this->stringParam('actu_titre', '');
        $params->ao_actu_content        = $this->stringParam('content', '');
        $params->ao_actu_mot_cle_id     = $this->intParam('motcle', 0);
        $params->ao_actu_date           = communSrv::toDateSql($this->stringParam('actu_date', ''));
        $params->ao_actu_bon_plan       = $this->intParam('bp', 0);
        $params->ao_actu_categorie_id   = $this->intParam('categorie', 0);
        $params->ao_actu_priority       = $this->intParam('priority', 0);
        $params->id                     = $id;
        $params->ao_actu_photo_1        = communSrv::uploadImage($_FILES['photo1'], ACTU_PATH_PHOTO);
        $params->ao_actu_photo_2        = communSrv::uploadImage($_FILES['photo2'], ACTU_PATH_PHOTO);
        $params->ao_actu_photo_3        = communSrv::uploadImage($_FILES['photo3'], ACTU_PATH_PHOTO);

   		$idCat = (int)$params->ao_actu_categorie_id;

       
        if ($id) {
            actuSrv::modifier($params);
        } else {
            actuSrv::ajouter($params);
        }

        $rep = $this->getResponse('redirect');
        $rep->action = 'actu~BoActu:index';
		$rep->params = array('idCat'=>$idCat);

        return $rep;
    }

}


